/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __util_timer_h
#define __util_timer_h 1

#include <boost/date_time/posix_time/posix_time_types.hpp>

using namespace boost::posix_time;

namespace galaxy {
    namespace util {

        /** This class is used to time synchronisation (e.g. for a thread).
        * If a thread loops every 40 seconds then this class can be used to measure the
        * time that the thread has already used so that it can adjust the 'sleep' time.
        * for instance:
        * \code
        * Timer timer;
        * while (status == RUNNING) {
        *   timer.updateTimer ();
        *   // .. do something .. //
        *   time_duration elapsed = timer.getElapsedDuration ();
        *   if (elapsed < milliseconds(10)) {
        *       boost::this_thread::sleep(milliseconds(10) - elapsed);
        *   }
        * }
        * \endcode
        */
        class Timer
        {
            ptime          timer;           /**< The most recent 'timestamp'. */
            boost::int64_t ticksPerSecond;  /**< The number of ticks in one second on the current processor */

        public:
            Timer(void);
            ~Timer(void);

            /** update the timer member (to the current time) */
            void updateTimer ();

            /** the elapsed time since the last updateTimer call */
            time_duration  getElapsedDuration ();
            /** the elapsed number of ticks since the last updateTimer call */
            boost::int64_t getElapsedTicks ();
        };

    } // namespace util
} // namespace galaxy

#endif // __util_timer_h
